<?php
session_name(DicomScience);
session_start();

/*
 * Aquire Server and Client Infrastructure Data
 */	
$_SESSION['remoteClient'] = $_SERVER['REMOTE_ADDR']; #Client Address
$_SESSION['serverDir'] = getcwd(); #Server Working Directory (location of index.php)

/*
 * Check Browser Type
 */	
$useragent = $_SERVER['HTTP_USER_AGENT'];

if (preg_match('|MSIE ([0-9].[0-9]{1,2})|',$useragent,$matched)) {
	
    $browser_version=$matched[1];
    $browser = 'IE';
	
} elseif (preg_match( '|Opera ([0-9].[0-9]{1,2})|',$useragent,$matched)) {
	
    $browser_version=$matched[1];
    $browser = 'Opera';
	
} elseif(preg_match('|Firefox/([0-9\.]+)|',$useragent,$matched)) {
	
        $browser_version=$matched[1];
        $browser = 'Firefox';
		
} elseif(preg_match('|Safari/([0-9\.]+)|',$useragent,$matched)) {
	
        $browser_version=$matched[1];
        $browser = 'Safari';
		
} else {
	
    // browser not recognized!
    $browser_version = 0;
    $browser= 'other';
	
}	
$_SESSION['remoteBrowser'] = $browser;
$_SESSION['remoteBrowserVersion'] = $browser_version;


/*
 * Check Language and Global settings
 */
if (!((file_exists('includes/gset.php') OR file_exists('gset.php')))){
	// redirect to installation if no settings are available
	
	Header("Location: install/index.php");
	exit(); 
		
} else {
	// read gset.php file into session array
	
		require_once('includes/gset.php');
		
		$_SESSION['imgstore'] = $img_store;
		$_SESSION['thbstore'] = $thb_store;
		$_SESSION['institute'] = $institute;
		$_SESSION['lang'] = $lang;
		$_SESSION['serv'] = $serv;
		$_SESSION['dbase'] = $dbase;
		$_SESSION['dbusr'] = $dbusr;
		$_SESSION['dbpwd'] = $dbpwd;
		$_SESSION['imgrep'] = $img_rep;
		$_SESSION['thbrep'] = $thb_rep;
		
}

/*
 * Load Theme settings
 */
require_once('./includes/gtheme.php');
$_SESSION['theme'] = $thdir;


/*
 * Load Language File
 */
 if ($lang == '1'){
	require('./includes/language/german.php');
} elseif ($lang == '2'){
	require('./includes/language/english.php');
} elseif ($lang == '3'){
	require('./includes/language/french.php');
} elseif ($lang == '4'){
	require('./includes/language/italian.php');
}
/*
 * Check Login Cookie and Login Type and Personal Settings
 */
if (! isset ($_COOKIE['DicomScienceCookie'])){
	
	# No "stay logged in" cookie detected. Assuming, no user is logged in.
	# Setting Guest Account Data

	if ( $_SESSION['userid'] == ""){
		
		$_SESSION['userid'] = 0;
		$_SESSION['userlevel'] = 0;
		$_SESSION['firstname'] = $nls_guest_firstname;
		$_SESSION['lastname'] = $nls_guest_lastname;
		$_SESSION['username'] = "Guest";
	
	}
	
} else {
	
	# Cookie detected. Aquiring Cookie information.
	
	$cookieData = explode("&",$_COOKIE['DicomScienceCookie']);
	
	$_SESSION['userid'] = $cookieData[0];
	$_SESSION['userlevel'] = $cookieData[1];
	$_SESSION['firstname'] = $cookieData[2];
	$_SESSION['lastname'] = $cookieData[3];
	$_SESSION['username'] = $cookieData[4];

}

/*
 * Check Platform for da iPhone
 */	
$useragent = $_SERVER['HTTP_USER_AGENT'];

if (preg_match('/mobile/i',$useragent,$matched)) {
	
	# Redirect to mobile view
	Header("Location: ./includes/mobile");
	exit();

} else {
	
	# Redirect to Desktop view
	Header("Location: ./includes/portalMain.php");
	exit();
	
}

?>